package com.rental.web.controller;

import com.rental.common.login.GetIdByToken;
import com.rental.common.result.AjaxResult;
import com.rental.common.util.AuthContextHolder;
import com.rental.model.entity.house.RentalHourse;
import com.rental.model.entity.user.UserInfo;
import com.rental.model.form.hourse.InsertRentalHourseForm;
import com.rental.model.form.hourse.RentalHourseForm;
import com.rental.model.vo.PageVo;
import com.rental.web.service.RentalHourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "出租房屋模块")
@RequestMapping("/rental-hourse")
public class RentalHourseController {

    @Autowired
    private RentalHourseService rentalHourseService;

    /***
     * 获取房屋列表
     * @return
     */
    @Operation(summary = "获取房屋列表")
    @GetMapping("/getRentalHourseList")
    public AjaxResult<List<RentalHourse>> getRentalHourseList() {
        return AjaxResult.success(rentalHourseService.getRentalHourseList());
    }

    /**
     * 根据查询条件获取房屋列表
     * @param rentalHourseForm
     * @return
     */
    @Operation(summary = "根据查询条件获取房屋列表")
    @PostMapping("/getRentalHourseListBySearch")
    public AjaxResult<PageVo<RentalHourse>> getRentalHourseListBySearch(@RequestBody RentalHourseForm rentalHourseForm){
        return rentalHourseService.getRentalHourseListBySearch(rentalHourseForm);
    }

    /**
     * 房屋详情
     * @param id,uid  id:房屋id uid:用户id(非房东id)
     * @return
     */
    @Operation(summary = "获取房屋详情")
    @GetIdByToken
    @GetMapping("/getRentalHourseDetail/{id}")
    public AjaxResult<RentalHourse> getRentalHourseDetail(@PathVariable Long id){
        Long uid = AuthContextHolder.getUserId();
        return rentalHourseService.getRentalHourseDetail(id,uid);
    }

    /**
     * 新增出租屋
     * @param insertRentalHourseForm
     * @return
     */
    @Operation(summary = "新增出租屋")
    @GetIdByToken
    @PostMapping("/insertRentalHourse")
    public AjaxResult<Boolean> insertRentalHourse(@RequestBody InsertRentalHourseForm insertRentalHourseForm){
        Long userId = AuthContextHolder.getUserId();
        insertRentalHourseForm.setUid(userId);
        return rentalHourseService.insertRentalHourse(insertRentalHourseForm);
    }

    /**
     * 获取房东微信
     * @param id,uid  id:房屋id uid:房东id
     * @return
     */
    @Operation(summary = "获取房东微信")
    @GetMapping("/getLandlordWechat/{id}")
    @GetIdByToken
    public AjaxResult<String> getLandlordWechat(@PathVariable Long id){
        Long uid = AuthContextHolder.getUserId();
        return rentalHourseService.getLandlordWechat(id,uid);
    }

    /**
     * 获取房东电话
     * @param id,uid  id:房屋id uid:房东id
     * @return
     */
    @Operation(summary = "获取房东电话")
    @GetMapping("/getLandlordPhone/{id}")
    @GetIdByToken
    public AjaxResult<String> getLandlordPhone(@PathVariable Long id){
        Long uid = AuthContextHolder.getUserId();
        return rentalHourseService.getLandlordPhone(id,uid);
    }

    /**
     * 下架房屋
     * @param id  id:房屋id
     * @return
     */
    @Operation(summary = "下架房屋")
    @GetMapping("/changeStatus/{id}")
    @GetIdByToken
    public AjaxResult<Boolean> changeStatus(@PathVariable Long id){
        Long uid = AuthContextHolder.getUserId();
        return rentalHourseService.changeStatus(id,uid);
    }

    @Operation(summary = "收藏房屋")
    @GetMapping("/collectHouse/{id}")
    @GetIdByToken
    public AjaxResult<Boolean> collectHouse(@PathVariable Long id){
        Long uid = AuthContextHolder.getUserId();
        return rentalHourseService.collectHouse(id,uid);
    }

    @Operation(summary = "取消收藏房屋")
    @GetMapping("/cancelCollectHouse/{id}")
    @GetIdByToken
    public AjaxResult<Boolean> cancelCollectHouse(@PathVariable Long id){
        Long uid = AuthContextHolder.getUserId();
        return rentalHourseService.cancelCollectHouse(id,uid);
    }

    @Operation(summary = "获取有效出租房源数")
    @GetMapping("/getValidRentalHourseCount")
    public AjaxResult<Long> getValidRentalHourseCount(){
        return rentalHourseService.getValidRentalHourseCount();
    }

    @Operation(summary = "举报")
    @GetMapping("/accusation/{id}")
    public AjaxResult<Boolean> accusation(@PathVariable Long id){
        return rentalHourseService.accusation(id);
    }

    @Operation(summary = "查询发布人信息")
    @GetMapping("/getPublishUserInfo/{uid}")
    public AjaxResult<UserInfo> getPublishUserInfo(@PathVariable("uid") Long id){
        return rentalHourseService.getPublishUserInfo(id);
    }
}
